<?php
class OptionTransferSelect extends CWidget{
	/**
	 * The label for the first mutiple select
	 * option
	 * @var string
	 */
	public $leftTitle;
	/**
	 * The label for the sencond mutiple select
	 * option
	 * @var string
	 */
	public $rightTitle;
	/**
	 * The name for the first mutiple select
	 * require
	 * @var string
	 */
	public $name;
	/**
	 * The name for the sencond mutiple select
	 * require
	 * @var string
	 */
	public $doubleName;
	/**
	 * data for generating the first list options .suport  (value=>display) and string.
	 * require
	 * @var array
	 */
	public $list;
	/**
	 * data for generating the first list options .suport  (value=>display) and string.
	 * require
	 * @var array
	 */
	public $doubleList;
	//***************************************************************************
	// register clientside widget files
	//***************************************************************************
	protected function registerClientScript(){
		$file=dirname(__FILE__).DIRECTORY_SEPARATOR.'jquery.multiselects.js';
		$jsFile=Yii::app()->getAssetManager()->publish($file);
		$cs=Yii::app()->clientScript;
 		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile($jsFile, CClientScript::POS_HEAD);
	}
	//***************************************************************************
	// Initializes the widget
	//***************************************************************************
	public function init(){
		if(!isset($this->name)){
			throw new CHttpException(500,'"name" have to be set!');
		}
		if(!isset($this->doubleName)){
			throw new CHttpException(500,'"doubleName" have to be set!');
		}
		if(!isset($this->list)){
			throw new CHttpException(500,'"list" have to be set!');
		}
		if(!isset($this->doubleList)){
			throw new CHttpException(500,'"doubleList" have to be set!');
		}
	}
	//***************************************************************************
	// Run the widget
	//***************************************************************************
	public function run(){
		echo "<table border=\"0\" style=\"width:500px\">\n";
		echo "<tr>\n";
		echo "<td>\n";
		if(isset($this->leftTitle)){
			echo "<label for=\"leftTitle\">{$this->leftTitle}</label><br />\n";
		}
		echo CHtml::listBox($this->name, '', $this->list, array(
			'id'=>'select_left',
			'multiple'=>'multiple',
			'size'=>10,
			'style'=>'width:200px'
		));
		echo "</td>\n";

		echo "<td style=\"text-align:center\">\n";
		echo "<input type=\"button\" style=\"width:40px;\" id=\"options_left\"	 value=\"&lt;-\" /><br />\n";
		echo "<input type=\"button\" style=\"width:40px;\" id=\"options_right\"	 value=\"-&gt;\" /><br />\n";
		echo "<input type=\"button\" style=\"width:40px;\" id=\"options_left_all\"	 value=\"&lt;&lt;--\" /><br />\n";
		echo "<input type=\"button\" style=\"width:40px;\" id=\"options_right_all\"	 value=\"--&gt;&gt;\" /><br /></td>\n";

		echo "<td>\n";
		if(isset($this->rightTitle)){
			echo "<label for=\"rightTitle\">{$this->rightTitle}</label><br />\n";
		}
		echo CHtml::listBox($this->doubleName, '', $this->doubleList, array(
			'id'=>'select_right',
			'multiple'=>'multiple',
			'size'=>10,
			'style'=>'width:200px'
		));
		echo "</td>\n";
		echo "</tr></table>\n";
		$this->registerClientScript();
		echo "<script type=\"text/javascript\"><!--\n";
		echo "\$(function() {\n";
		echo "\$(\"#select_left\").multiSelect(\"#select_right\", {trigger: \"#options_right\"});\n";
		echo "\$(\"#select_right\").multiSelect(\"#select_left\", {trigger: \"#options_left\"});\n";
		echo "\$(\"#select_left\").multiSelect(\"#select_right\", {allTrigger:\"#options_right_all\"});\n";
		echo "\$(\"#select_right\").multiSelect(\"#select_left\", {allTrigger:\"#options_left_all\"});\n";
		echo "});\n";
		echo "// --></script>\n";
		parent::init();
	}

	protected function renderContent(){
	}
}
?>